-- 牵制技能定义（可无限触发）
local boqianzhi = fk.CreateSkill{
  name = "boqianzhi",
}

Fk:loadTranslationTable{
  ["boqianzhi"] = "牵制",
  [":boqianzhi"] = "其他角色摸牌阶段开始时，你可以交给其一张牌，然后此回合的弃牌阶段结束时，你获得其于此阶段所有弃置的牌。",
  ["#boqianzhi-invoke"] = "牵制：你可以将1张牌交给 %dest，本回合弃牌阶段获得其弃置的牌",
  ["$boqianhzi1"] = "惠用张仪，昭得范雎，朕拥卿足矣！",
  ["$boqianhzi2"] = "南靖交越，北复荆襄，使吴成帝业。",

}

-- 摸牌阶段开始时的触发效果
boqianzhi:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    -- 触发条件：
    -- 1. 玩家拥有此技能
    -- 2. 目标角色处于摸牌阶段
    -- 3. 玩家手牌数≥2
    -- 4. 目标角色未死亡
    return player:hasSkill(boqianzhi.name) and 
           target.phase == Player.Draw and
           #player:getCardIds("he") > 1 and 
           not target.dead
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    -- 询问玩家是否交出两张牌
    local cards = room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      include_equip = true,  -- 可包含装备区
      skill_name = boqianzhi.name,
      cancelable = true,     -- 可取消
      prompt = "#boqianzhi-invoke::" .. target.id  -- 提示目标ID
    })
    if #cards == 1 then
      -- 记录选择的牌和目标
      event:setCostData(self, {tos = {target}, cards = cards})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    -- 记录目标角色ID（用于后续弃牌阶段回收）
    room:setPlayerMark(player, "boqianzhi-target", target.id)
    -- 将选择的两张牌交给目标
    room:moveCardTo(event:getCostData(self).cards, Card.PlayerHand, target, fk.ReasonGive, boqianzhi.name, nil, true, player)
  end,
})

-- 弃牌阶段结束时的触发效果（延迟效果）
boqianzhi:addEffect(fk.EventPhaseEnd, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    -- 触发条件：
    -- 1. 目标角色处于弃牌阶段结束时
    -- 2. 玩家未死亡
    -- 3. 目标是之前使用技能指定的角色
    if target.phase == Player.Discard and not player.dead then
      local targetId = player:getMark("boqianzhi-target") or 0
      if targetId == target.id then
        -- 收集目标在本阶段弃置的所有牌
        local cards = {}
        player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
          for _, move in ipairs(e.data) do
            if move.from == target and move.moveReason == fk.ReasonDiscard then
              for _, info in ipairs(move.moveInfo) do
                table.insertIfNeed(cards, info.cardId)
              end
            end
          end
        end, Player.HistoryPhase)
        
        -- 过滤出确实进入弃牌堆的牌
        cards = table.filter(cards, function(id)
          return table.contains(player.room.discard_pile, id)
        end)
        
        if #cards > 0 then
          -- 记录可回收的牌
          event:setCostData(self, {cards = cards})
          return true
        end
      end
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    -- 将目标弃置的牌全部移至玩家手牌
    player.room:moveCardTo(event:getCostData(self).cards, Card.PlayerHand, player, fk.ReasonJustMove, boqianzhi.name, nil, true, player)
  end,
})

return boqianzhi